﻿import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:simu_studio/app/modules/home/controllers/home_controller.dart';
import 'package:simu_studio/app/modules/home/views/home_device_chart.dart';
import 'package:simu_studio/app/pages/page_mixin.dart';

class HomeDevicesView extends StatelessWidget with PageMixin {
  @override
  Widget build(BuildContext context) {
    HomeController controller = Get.find();
    return Obx(() {
      if (controller.avaliableDevices.isEmpty) {
        return Center(
          child: Text(
            'Home_Text_NoDeviceData'.tr,
            style: const TextStyle(fontSize: 30, color: Color(0x00ff0000)),
          ),
        );
      } else {
        return ConstrainedBox(
          constraints: const BoxConstraints.expand(),
          child: TabView(
            currentIndex: controller.deviceTabIndex.value,
            onChanged: (index) {
              controller.deviceTabIndex.value = index;
            },
            // ignore: invalid_use_of_protected_member
            tabs: _generateTabs(controller.avaliableDevices),
            tabWidthBehavior: TabWidthBehavior.sizeToContent,
            closeButtonVisibility: CloseButtonVisibilityMode.never,
            onNewPressed: null,
          ),
        );
      }
    });
  }

  List<Tab> _generateTabs(List<String> devices) {
    List<Tab> tabs = [];
    var deviceTabs = devices.map((e) {
      return Tab(
        text: Text(e),
        body: HomeDeviceChartView(
          deviceName: e,
        ),
        icon: const Icon(FluentIcons.chart),
      );
    }).toList();
    tabs.addAll(deviceTabs);
    return tabs;
  }
}
